import xml.etree.ElementTree as ET
import os
from shutil import copyfile
"""
basedir = os.path.abspath(os.path.dirname(__file__))
current_img_path="img"
current_xml_path="img"
project_img_path="/home/lselie/Downloads/keras-yolo-master"
img_abs_path=os.path.join(basedir,current_img_path)
xml_abs_path=os.path.join(basedir,current_xml_path)
print(basedir)
print(img_abs_path)
print(xml_abs_path)
a=100
for file in os.listdir(img_abs_path):

    signal=os.path.splitext(file)[1]
    print(signal)
    if signal==".jpg":
    #print(filename)
        cur_file_name = os.path.join(current_img_path, file)
        new_file_name = os.path.join(current_img_path, str(a-100).zfill(4) + signal)
        os.rename(cur_file_name, new_file_name)
        a = a + 1
        filename = os.path.splitext(file)[0]
        xml_name=filename+".xml"
        print(xml_name)
        xmlfile_abs_path=os.path.join(xml_abs_path,xml_name)
        print(xmlfile_abs_path)
        tree=ET.parse(xmlfile_abs_path)
        root = tree.getroot()
        project_file_path=os.path.join(project_img_path,filename)
        root[2].text=project_file_path
        print("tree type:", type(tree))
"""


basedir = os.path.abspath(os.path.dirname(__file__))
current_class="mask"
#current_img_path="img"
#current_xml_path="img"
xml_abs_path="/home/leslie/Downloads/keras-yolo3-master/img"
img_abs_path="/home/leslie/Downloads/keras-yolo3-master/img"




project_img_path="/home/leslie/Downloads/keras-yolo3-master/VOCdevkit/VOC2007/JPEGImages"
project_xml_path="/home/leslie/Downloads/keras-yolo3-master/VOCdevkit/VOC2007/Annotations"
#img_abs_path=os.path.join(basedir,current_img_path)
#xml_abs_path=os.path.join(basedir,current_xml_path)
print(basedir)
print(img_abs_path)
print(xml_abs_path)
for file in os.listdir(img_abs_path):
    file_name=os.path.splitext(file)[0]
    signal=os.path.splitext(file)[1]
    print(signal)
    if signal==".jpg":
        new_name=current_class+ file_name + signal
        new_xml_name = current_class + file_name + ".xml"
        new_file_name = os.path.join(project_img_path, new_name)

        cur_file_name = os.path.join(img_abs_path, file)
        #os.rename(cur_file_name, new_file_name)
        #des_file_name = os.path.join(project_img_path, file)
        copyfile(cur_file_name,new_file_name)
        xml_name = file_name + ".xml"
        op_xml_path = os.path.join(xml_abs_path , xml_name)
        tree = ET.parse(op_xml_path)
        root = tree.getroot()
        #project_file_path = os.path.join(project_img_path, file_name)
        root[1].text = new_name
        #print(new_file_name)
        root[2].text = new_file_name
        #new_xml_name = new_name
        tree.write(op_xml_path)
        new_xml_name = os.path.join(project_xml_path, new_xml_name)
        cur_xml_name = os.path.join(xml_abs_path, xml_name)
        # os.rename(cur_file_name, new_file_name)
        # des_file_name = os.path.join(project_img_path, file)
        copyfile(cur_xml_name, new_xml_name)
        #os.system("cp ")
#os.system("cp ")
#xml_file = "VOCdevkit/VOC2007/Annotations/0001.xml"

#tree = ET.parse(xml_file)
#print("tree type:", type(tree))

# 获得根节点
#root = tree.getroot()
#print("root type:", type(root))
#print(root.tag, "----", root.attrib)
#print(type(root.tag))

# 使用下标访问
#print(root[1].text)
#print(root[2].text)

#root[1].text="00001.jpg"
#root[2].text="00001.jpg"


#tree.write(xml_file)


